package com.xqkad.myplayer.service;

import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.RemoteException;
import android.util.Log;

import com.xqkad.myplayer.MediaAidlInterface;
import com.xqkad.myplayer.info.MusicInfo;

import java.util.List;

public class MusicPlayer {
    private static final String TAG = "mytag";
    public static MediaAidlInterface mService;

    public static final void bindService(final Context context, final ServiceConnection conn){
        final Intent intent = new Intent(context,MediaService.class);
        context.startService(intent);
        context.bindService(intent,conn,Context.BIND_AUTO_CREATE);
    }

    public static final void unbindService(Context context, ServiceConnection conn){
        context.unbindService(conn);
    }

    public static final void open(List<MusicInfo> list, int position){
        try {
            if(mService!=null)
                mService.open(list,position);
            else
                Log.d(TAG, "open: mServiceNull");
        } catch (Exception e) {
            Log.d(TAG, "open: error");
        }
    }

    public static final void play(){
        if(mService != null) {
            try {
                mService.play();
            } catch (Exception e) {}
        }
    }

    public static final void pause(){
        if(mService != null) {
            try {
                mService.pause();
            } catch (Exception e) {}
        }
    }

    public static final void previous(){
        if(mService != null) {
            try {
                mService.previous();
            } catch (Exception e) {}
        }
    }

    public static final void next(){
        if(mService != null) {
            try {
                mService.next();
            } catch (Exception e) {}
        }
    }

    public static final void stop(){
        if(mService != null) {
            try {
                mService.stop();
            } catch (Exception e) {}
        }
    }

    public static void seek(final int position) {
        if (mService != null) {
            try {
                mService.seek(position);
            } catch (final RemoteException ignored) {
            }
        }
    }

    public static final boolean isPlaying(){
        if(mService != null) {
            try {
                return mService.isPlaying();
            } catch (Exception e) {}
        }
        return false;
    }

    public static final int getDuration(){
        if(mService != null) {
            try {
                return mService.getDuration();
            } catch (Exception e) {}
        }
        return -1;
    }

    public static final int getCurrentPosition(){
        if(mService != null) {
            try {
                return mService.getCurrentPosition();
            } catch (Exception e) {}
        }
        return -1;
    }

    public static final int getPosition(){
        if(mService != null) {
            try {
                return mService.getPosition();
            } catch (Exception e) {}
        }
        return -1;
    }

    public static final String getTitle(){
        if(mService != null) {
            try {
                return mService.getTitle();
            } catch (Exception e) {}
        }
        return "无标题";
    }

    public static String getMusicId() {
        if(mService != null) {
            try {
                return mService.getMusicId();
            } catch (Exception e) {}
        }
        return "-1";
    }

    public static int getPlayMode() {
        if(mService != null) {
            try {
                return mService.getPlayMode();
            } catch (Exception e) { }
        }
        return -1;
    }

    public static void setPlayMode(int mode) {
        if(mService != null) {
            try {
                mService.setPlayMode(mode);
            } catch (Exception e) { }
        }
    }

    public static void playlist() {

    }
}
